package com.xzx.study.animation;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.xzx.study.R;

public class ViewAnimationActivity extends AppCompatActivity implements View.OnClickListener {

    private Button code_scale_btn;
    private Button xml_scale_btn;
    private Button code_rotate_btn;
    private Button xml_rotate_btn;
    private Button code_alpha_btn;
    private Button xml_alpha_btn;
    private Button code_translation_btn;
    private Button xml_translation_btn;
    private Button code_animation_set_btn;
    private Button xml_animation_set_btn;

    private ImageView iv_animation;
    private TextView tv_animation_msg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_animatin);

        code_scale_btn = findViewById(R.id.code_scale_btn);
        code_scale_btn.setOnClickListener(this);

        xml_scale_btn = findViewById(R.id.xml_scale_btn);
        xml_scale_btn.setOnClickListener(this);

        code_rotate_btn = findViewById(R.id.code_rotate_btn);
        xml_rotate_btn = findViewById(R.id.xml_rotate_btn);
        code_alpha_btn = findViewById(R.id.code_alpha_btn);
        xml_alpha_btn = findViewById(R.id.xml_alpha_btn);
        code_translation_btn = findViewById(R.id.code_translation_btn);
        xml_translation_btn = findViewById(R.id.xml_translation_btn);
        code_animation_set_btn = findViewById(R.id.code_animation_set_btn);
        xml_animation_set_btn = findViewById(R.id.xml_animation_set_btn);

        code_rotate_btn.setOnClickListener(this);
        xml_rotate_btn.setOnClickListener(this);
        code_alpha_btn.setOnClickListener(this);
        xml_alpha_btn.setOnClickListener(this);
        code_translation_btn.setOnClickListener(this);
        xml_translation_btn.setOnClickListener(this);
        code_animation_set_btn.setOnClickListener(this);
        xml_animation_set_btn.setOnClickListener(this);

        iv_animation = findViewById(R.id.iv_animation);
        tv_animation_msg = findViewById(R.id.tv_animation_msg);
    }

    @Override
    public void onClick(View v) {
        if (code_scale_btn == v) {
            // Code缩放动画: 宽度从0.5到1.5, 高度从0.0到1.0, 缩放的圆心为顶部中心点,延迟1s开始,持续2s,最终还原
            ScaleAnimation scaleAnimation = new ScaleAnimation(0.5f, 1.0f, 0, 1.0f, Animation.RELATIVE_TO_SELF, 0.5f,
                                                               Animation.RELATIVE_TO_SELF, 0);
            scaleAnimation.setDuration(2000);
            scaleAnimation.setStartOffset(1000);
            scaleAnimation.setFillBefore(true);
            iv_animation.startAnimation(scaleAnimation);
        } else if (xml_scale_btn == v) {
            Animation animation = AnimationUtils.loadAnimation(this, R.anim.scale_test);
            iv_animation.startAnimation(animation);
        } else if (code_rotate_btn == v) {
            // Code旋转动画: 以图片中心点为中心, 从负90度到正90度, 持续5s
            RotateAnimation animation = new RotateAnimation(-90f, 90f, Animation.RELATIVE_TO_SELF, 0.5f,
                                                            Animation.RELATIVE_TO_SELF, 0.5f);
            animation.setDuration(5000);
            iv_animation.startAnimation(animation);

        } else if (xml_rotate_btn == v) {
            Animation animation = AnimationUtils.loadAnimation(this, R.anim.rotate_test);
            iv_animation.startAnimation(animation);
        } else if (code_alpha_btn == v) {
            // Code透明度动画: 从完全透明到完全不透明, 持续2s
            AlphaAnimation animation = new AlphaAnimation(0f, 1f);
            animation.setDuration(2000);
            iv_animation.startAnimation(animation);
        } else if (xml_alpha_btn == v) {
            Animation animation = AnimationUtils.loadAnimation(this, R.anim.alpha_test);
            iv_animation.startAnimation(animation);
        } else if (code_translation_btn == v) {
            // Code移动动画: 向右移动一个自己的宽度, 向下移动一个自己的高度, 持续2s
            TranslateAnimation animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f,
                                                                  Animation.RELATIVE_TO_SELF, 1f,
                                                                  Animation.RELATIVE_TO_SELF, 0f,
                                                                  Animation.RELATIVE_TO_SELF,1f);
            animation.setDuration(2000);
            iv_animation.startAnimation(animation);

        } else if (xml_translation_btn == v) {
            Animation animation = AnimationUtils.loadAnimation(this, R.anim.translate_test);
            iv_animation.startAnimation(animation);
        } else if (code_animation_set_btn == v) {
            // Code复合动画: 透明度从透明到不透明, 持续2s, 接着进行旋转360度的动画, 持续1s
            AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1);
            alphaAnimation.setDuration(2000);

            RotateAnimation rotateAnimation = new RotateAnimation(0f, 360f);
            rotateAnimation.setStartOffset(2000);
            rotateAnimation.setDuration(1000);

            AnimationSet animationSet = new AnimationSet(false);
            animationSet.addAnimation(alphaAnimation);
            animationSet.addAnimation(rotateAnimation);
            iv_animation.startAnimation(animationSet);

        } else if (xml_animation_set_btn == v) {
            Animation animation = AnimationUtils.loadAnimation(this, R.anim.set_test);
            iv_animation.startAnimation(animation);
        }
    }
}
